﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.BLL
{
    public class CommodityCategoryBLL : BaseBLL<CommodityCategoryBLL>
    {
        #region 默认方法

        /// <summary>
        ///     增加一条数据
        /// </summary>
        public int Add(CommodityCategory model)
        {
            return CommodityCategoryDAL.Instance.Add(model);
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update(CommodityCategory model)
        {
            return CommodityCategoryDAL.Instance.Update(model);
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return CommodityCategoryDAL.Instance.Delete(Id);
        }

        /// <summary>
        ///     批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return CommodityCategoryDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public CommodityCategory GetModel(int Id)
        {
            DataSet ds = CommodityCategoryDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<CommodityCategory>(ds).ToList()[0];
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public List<CommodityCategory> GetModelList(string strWhere = null)
        {
            DataSet ds = CommodityCategoryDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<CommodityCategory>(ds).ToList();
        }

        #endregion

        #region 扩展方法

        /// <summary>
        ///     获得分页数据列表
        /// </summary>
        public TableDataViewDto<ViewCommodityCategoryDto> GetPagingList(List<MagModules> magModules, int draw, int pageIndex, int pageSize,
            string searchKeyWord, string colOrder)
        {
            try
            {
                int totalCount = 0;
                List<CommodityCategory> list = CommodityCategoryDAL.Instance.GetPagingList(pageIndex, pageSize,
                    searchKeyWord, colOrder, ref totalCount);
                List<ViewCommodityCategoryDto> theReturnList = list.Select(cc => new ViewCommodityCategoryDto
                {
                    CategoryName = cc.CategoryName,
                    Description = cc.Description,
                    Id = cc.Id
                }).ToList();
                theReturnList.ForEach(s =>
                {
                    if (magModules.FindAll(t => t
                        .Action == "Update" && t
                            .Controller == "CommodityCategory").Count > 0)
                    {
                        s.Action +=
                            "<a class='btn btn-warning' href='javascript:;' onclick=updateData(" + s.Id + ");>修改</a>  ";
                    }

                    if (magModules.FindAll(t => t
                       .Action == "Delete" && t
                           .Controller == "CommodityCategory").Count > 0)
                    {
                        s.Action +=
                            "<a class='btn btn-danger' href='javascript:;' onclick=deleteData(" + s.Id + ");>删除</a> ";
                    }
                });
                var returnDataList = new TableDataViewDto<ViewCommodityCategoryDto>
                {
                    data = theReturnList,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };

                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }

        /// <summary>
        ///     获取商家在售商品的商品分类列表
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="ignoreOffSelling"></param>
        /// <returns></returns>
        public List<CommodityCategory> GetCateListByShopId(int shopId, bool ignoreOffSelling)
        {
            return CommodityCategoryDAL.Instance.GetCateListByShopId(shopId, ignoreOffSelling);
        }

        #endregion
    }
}